import {createRouter, createWebHashHistory, createWebHistory} from 'vue-router'
// 开启历史模式
// vue2中使用的mode：history 实现
// const routerHistory = createWebHashHistory();
const routerHash = createWebHashHistory();
// @ts-ignore
const router = createRouter({
    history: routerHash,
    // 这里分为面试和博客，2种路由
    // interview  blog
    routes: [
        {
            path: '/',
            redirect: '/home'
        },
        {
            path: '/interview',
            redirect: '/interview'
        },
        {
            path: '/blog',
        },
        {
            path: '/grid',
            redirect: '/home',
            component: () => import('../views/test.vue')
        },
        {
            path: '/test',
            component: () => import('../views/test.vue')
        },
        {
            path: '/home',
            name: 'home',
            component: () => import('../views/home.vue')
        },
        {
            path: '/about',
            component: () => import('../views/about.vue')
        },
        {
            path: '/css',
            component: () => import('../views/css/selector.vue')
        },
        {
            path: '/mouse',
            component: () => import('../views/css/mouse.vue')
        },
    ]
})
// 动态路由刷新后test丢失的问题
router.beforeEach((to, from, next) => {
    if (router.hasRoute('test')) {
        next()
    } else {
        router.addRoute({
            path: '/test',
            name: 'test',
            component: () => import('../views/test.vue')
        })
        console.log('router.getRoutes', router.getRoutes())
        next(to)
    }

})
export default router
